import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';
import a006 from './a006';

export default async function a007(fd: IFeidaoAiBrowserComponent) {
	// todo
	// data - id="plan_name"
	const plan_name = fd.data.node.querySelector<HTMLInputElement>('[data-id="plan_name"]').value;
	if (!plan_name) {
		alert('请输入计划名称');
		return;
	}

	const plan_node = fd.data.node.querySelector<HTMLSelectElement>('[data-id="plan"]');
	const plan = plan_node.value;
	// console.log(plan);
	let plan_name1 = '';
	if (plan === '请选择') {
		alert('请先选择计划分类属性名称');
		return;
	} else {
		plan_name1 = plan_node.selectedOptions[0].text;
		// console.log(plan_name);
	}

	// data-id="type"
	const type_node = fd.data.node.querySelector<HTMLSelectElement>('[data-id="type"]');
	const type = type_node.value;
	// console.log(type);
	let type_name = "";
	if (type === '请选择') {
		alert('请先选择计划类型名称');
		return;
	} else {
		type_name = type_node.selectedOptions[0].text;
		// console.log(type_name);
	}

	// data-id="start_date"
	const start_date = fd.data.node.querySelector<HTMLInputElement>('[data-id="start_date"]').value;
	if (!start_date) {
		alert('请输入计划开始时间');
		return;
	}
	console.log('start_date', start_date);
	// data-id="end_date"
	const end_date = fd.data.node.querySelector<HTMLInputElement>('[data-id="end_date"]').value;
	if (!end_date) {
		alert('请输入计划结束时间');
		return;
	}
	console.log('end_date', end_date);
	// data-id="plan_location"
	const plan_location = fd.data.node.querySelector<HTMLInputElement>('[data-id="plan_location"]').value;
	if (!plan_location) {
		alert('请输入计划地点');
		return;
	}
	// data-id="plan_mark"
	let plan_mark: string = '';
	const p1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="plan_mark0"]');
	const p2 = fd.data.node.querySelector<HTMLInputElement>('[data-id="plan_mark1"]');
	const p3 = fd.data.node.querySelector<HTMLInputElement>('[data-id="plan_mark2"]');
	const plan_cycle_node = fd.data.node.querySelector<HTMLSelectElement>('[data-id="plan_cycle"]');
	const plan_cycle = plan_cycle_node.value;
	let plan_cycle_name = "";
	const request_date_node = fd.data.node.querySelector<HTMLSelectElement>('[data-id="request_date"]');
	const request_date = request_date_node.value;
	let request_date_name = "";
	if (p1.checked) {
		plan_mark = p1.value;
		if (plan_cycle === '请选择') {
			alert('请先选择计划周期');
			return;
		} else {
			plan_cycle_name = plan_cycle_node.selectedOptions[0].text;
			request_date_name = '';
			console.log(plan_cycle_name, request_date_name);
		}
	} else if (p2.checked) {
		plan_mark = p2.value;
		if (request_date === '请选择') {
			alert('请先选择关键日期');
			return;
		} else {
			request_date_name = request_date_node.selectedOptions[0].text;
			plan_cycle_name = '';
			console.log(plan_cycle_name, request_date_name);
		}
	} else if (p3.checked) {
		plan_mark = p3.value;
		if (plan_cycle === '请选择') {
			alert('请先选择计划周期');
			return;
		} else {
			plan_cycle_name = plan_cycle_node.selectedOptions[0].text;
			request_date_name = '';
			console.log(plan_cycle_name, request_date_name);
		}
	} else {
		alert('请先选择计划标识');
		return;
	}
	console.log(plan_mark);

	// data-id="plan_cycle"


	// data-id="request_date"


	// data-id="call_period"
	const call_period_node = fd.data.node.querySelector<HTMLSelectElement>('[data-id="call_period"]');
	const call_period = call_period_node.value;
	// console.log(call_period);
	let call_period_name = "";
	if (call_period === '请选择') {
		alert('请先选择调用期');
		return;
	} else {
		call_period_name = call_period_node.selectedOptions[0].text;
		console.log('call_period', call_period_name);
	}
	// data-id="plan_describe"
	const plan_describe = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="plan_describe"]').value;
	if (!plan_describe) {
		alert('请输入计划描述');
		return;
	}
	console.log('plan_describe', plan_describe);

	const plan_list_info = get(fd, 'plan_list') as { plan_list_no: string; plan_list_name: string; };
	if (!plan_list_info) {
		alert('请先选择计划清单');
		return;
	}

	const r = await nodejs<{ code: 0 | 1; result: string; }>('plan-cb/zj-000002/s003', {
		plan_name,
		plan_describe,
		plan_type_no: type,
		plan_type_name: type_name,
		plan_cla_attr_no: plan,
		plan_cla_attr_name: plan_name1,
		start_date,
		end_date,
		call_period: Number(call_period_name),
		plan_implement_status: 200,
		take_effect: 1,
		plan_location,
		plan_mark: Number(plan_mark),
		plan_cycle: plan_cycle_name,
		request_date: request_date_name,

		plan_list: plan_list_info
	});
	if (r.code === 1) {
		alert('保存成功');
		await a006(fd);
		refresh();
	} else {
		alert('保存失败');
	}

}
